/**
 * @file: common.h
 * @author: daizongh
 * @date: 2009/02/25
 * $Id$ 
 * 
 * @desc:  *** add description here *** 
 */

#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdint.h>

/* define EXTERN_C_BEGIN/EXTERN_C_END. */
#ifdef __cplusplus
#  define EXTERN_C_BEGIN extern "C" {
#  define EXTERN_C_END }
#else  //__cplusplus
#  define EXTERN_C_BEGIN 
#  define EXTERN_C_END
#endif //__cplusplus

/* define BOOLEAN. */
#define BOOL int
#define TRUE 1
#define FALSE 0

/* define common ret_t type and value. */
enum _ret_t {
	r_succeed = 0,
	r_failed = 1,
	r_nullpoint,
	r_enomem,
	r_outofbounds,
	r_einval,
	r_epthread,
	r_uninitialized,
	r_eintr,
	r_eio,
	r_eaccess,
	r_eagain,
	r_eof,
	r_timeout,
	r_ignore
};
typedef enum _ret_t ret_t;

/* define likely/unlikely/expected. */
#if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
#  define likely(x)       __builtin_expect(!!(x),1)
#  define unlikely(x)     __builtin_expect(!!(x),0)
#  define expected(x,y)   __builtin_expect((x),(y))
#else //__GNUC__ > 2 || __GNUC_MINOR__ >= 96
#  define likely(x)       (x)
#  define unlikely(x)     (x)
#  define expected(x,y)   (x)
#endif //__GNUC__ > 2 || __GNUC_MINOR__ >= 96

/* define namespace. */
#ifdef __cplusplus
#define NS_BEGIN namespace schedule {
#define NS_END }
#else //__cplusplus
#define NS_BEGIN 
#define NS_END
#endif //__cplusplus
#define NS schedule

/* define for close assign operator and copy constructor. */
#define COPY_CONSTRUCTOR(T) \
	T(const T &); \
T & operator=(const T &);

/* define STR. */
#define _STR(x) # x
#define STR(x) _STR(x)

/* define SAFE_STRING */
#define SAFE_STRING(x) (x ? x : "(null)")
#define SAFE_STRING_LENGTH(x) (x ? strlen(x) : 0)
#define SAFE_STRING_EQUAL(x,y) (x && y ? (strcmp(x, y) == 0) : (x == y))

/* common define. */
#ifndef PATH_MAX
#define PATH_MAX 511
#endif //PATH_MAX

#ifndef NULL
#define NULL 0
#endif //NULL

#define DEFAULT_BUFFER_SIZE 64
#define DEFAULT_RWBUFFER_SIZE 4096
#define DEFAULT_SOCK_BACKLOG 256

#define COMMON_FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

/* namespace define. */
#ifdef __cplusplus

#define COMMON common
#define COMMON_BEGIN namespace common {
#define COMMON_END }

#define SCHE schedule
#define SCHE_BEGIN namespace schedule {
#define SCHE_END }

#define UTIL util
#define UTIL_BEGIN namespace util {
#define UTIL_END }
#define UTIL_SIGN util

#define BLEND blend
#define BLEND_BEGIN namespace blend {
#define BLEND_END }

#define CLUSTERMAP clustermap
#define BEGIN_NAME_CLUSTERMAP namespace clustermap {
#define END_NAME_CLUSTERMAP }

#else 

#define COMMON
#define COMMON_BEGIN
#define COMMON_END

#define SCH
#define SCHE_BEGIN
#define SCHE_END

#define UTIL
#define UTIL_BEGIN
#define UTIL_END

#define BLEND
#define BLEND_BEGIN
#define BLEND_END

#endif //__cplusplus

#endif //_COMMON_H_
